package com.puppet.building182.service.impl;

import com.puppet.building182.handler.MyAssert;
import com.puppet.building182.mapper.UserMapper;
import com.puppet.building182.pojo.User;
import com.puppet.building182.pojo.VO.UseVO;
import com.puppet.building182.pojo.VO.UserVo;
import com.puppet.building182.pojo.enums.BuildingCode;
import com.puppet.building182.service.RoleService;
import com.puppet.building182.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
 private UserMapper userMapper;
@Autowired
private RoleService roleService;
    @Override
    public List<UserVo> getUser() {
        return userMapper.getUser();
    }

    @Override
    public Integer deleteById(Integer id) {
//        Integer num = houseService.countHouseByBuildingId(buildingId);
//        MyAssert.error(num > 0, BuildingCode.DELETE_BUILD_FAIL.getMessage());
//        return buildingMapper.deleteBuildingById(buildingId);
        Integer num=roleService.countRoleByUserId(id);
        MyAssert.error(num > 0, BuildingCode.DELETE_USER_FAIL.getMessage());
        return userMapper.deleteById(id);

    }

    @Override
    public UserVo getUserById(Integer id) {
        return userMapper.getUserById(id);
    }

    @Override
    public Integer updateUser(User user) {
        return userMapper.updateUser(user);
    }

    @Override
    public User login(UseVO users) {
        User user=userMapper.login(users);
        MyAssert.error(user ==null, BuildingCode.LOGIN_FAIL.getMessage());
        MyAssert.error(user.getStatus()==0, BuildingCode.LOGIN_FAIL.getMessage());
//        String password= String.valueOf(userMapper.getUserById(Integer.valueOf(users.getPassword())));
//        String password1=users.getPassword();
//        MyAssert.error(!password.equals(password1), BuildingCode.LOGIN_FAIL.getMessage());
        return user;

    }

    @Override
    public String updateAllUser(User users) {
//        名字校验
        String username = users.getUsername();
        User user = userMapper.getUserByUsername(username);
        if (user != null) {
            return "修改用户名已存在";
        }
        Integer i = userMapper.updateAllUser(users);
        if (i > 0) {
            return "更新用户信息成功";
        }else {
            return "更新失败，请联系管理员~";
        }

    }
}
